import { IndexModel } from "../../models/IndexModel.js"
let indexModel = new IndexModel()
let locat = require('../../utils/Location.js');

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
      bannerData: [],//轮播图数据
      themes:[],//主题列表

      scrollTop: 0, //默认距离顶部为0
      floorstatus: false,// 判断按钮是否显示
      swiperIndex: 0, //轮播指示点
      product_list: [], //产品数据
      filename:''
  },
  swiperChange:function(e) {
    const that = this;
    that.setData({
      swiperIndex: e.detail.current,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;           
    that._init()
    that.loadInfo()
  },
  _init: function () {
    //轮播图
    indexModel.getBanner(res => {
      console.log(res)
      this.setData({
        bannerData: res.result.data.data 
      })
    })
    // 主题
    indexModel.getTheme(res => {
      console.log(res)
      this.setData({
        themes: res.result.data.data
      })
    })
  },
  
  //查看商品详情
  goDetail: function (e) {
    console.log(e)
    let res = e.currentTarget.dataset
    let id = res.id;
    app.globalData.options = {rightNav:id,curNav:id}
    app.globalData.index = res.index;
    wx.switchTab({
      url:'../category/category?id=' + id,
      success(){
          let page=getCurrentPages().pop();
          console.log(page)
          if(page==undefined || page==null){
              return;
          }
          page.onLoad();
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

// 获取滚动条当前位置
onPageScroll: function (e) {
  // console.log(e)
  this.setData({
    scrollTop: e.scrollTop,
});
  if (e.scrollTop > 100) {
    this.setData({
      floorstatus: true
    });
  } else {
    this.setData({
      floorstatus: false
    });
  }
},

//回到顶部
goTop: function (e) {  // 一键回到顶部
  if (wx.pageScrollTo) {
    wx.pageScrollTo({
      scrollTop: 0
    })
  } else {
    wx.showModal({
      title: '提示',
      content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
    })
  }
},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (res) {
    let that = this
    // 获取购物车数据，显示TabBarBadge
    // TOOLS.showTabBarBadge()
  },
//获取当前位置的经纬度
loadInfo: function(res){
  let add = res
  let that=this;
  wx.getLocation({
    type: 'gcj02', //返回可以用于wx.openLocation的经纬度
    success: function (res) {
      let lat = res.latitude//维度
      let lon = res.longitude//经度
      return new Promise(function (resolve, reject) {
        locat.analyzeGps(lat, lon).then((res) => {
          console.log(res);
          let street = res.district + res.street
          let cityName = res.city
          let reg = new RegExp("市");
          let city = res.city.replace(reg,"");
          //返回API天气实况
          wx.request({
            url: 'https://tianqiapi.com/api?version=v1&appid=16738292&appsecret=3IJpYHCt',
            data: {
              'city': city
            },
            method: 'GET',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
              console.log(res)
              let obj = {city:cityName,street:street,data:res.data.data}
              wx.setStorageSync('obj', obj)
              console.log(obj)
              that.setData({
                street:street
              })
              if(add == 1){
                wx.showToast({
                  title: "授权成功",
                  icon: "none",
                })
              }
            }
          })
        
          resolve(res);
        });
      })

    }
  })
},
// 打开定位功能
toLocation() {
  let that = this
  wx.getSetting({
    success(res) {
      if (!res.authSetting["scope.userLocation"]) { //未授权走这一步
        wx.showModal({
          title: '温馨提示',
          content: '检测到您没打开定位权限，是否去设置打开？',
          showCancel: true,
          cancelText: '取消',
          cancelColor: '#000000',
          confirmText: '确定',
          confirmColor: '#3CC51F',
          success: async(result) => {
              if (result.confirm) {
                wx.openSetting({
                  success: (res) => {
                    let add = 1
                    that.loadInfo(add)
                   }
                })
              }else if(result.cancel) {
              
                return;
              }
          }
        });
      }else{

      }
    }
  })
},
// 跳转天气实况
weatherPrediction:function(e){
  wx.navigateTo({
    url: "./weather"
  })
},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})